package hotel.callback.oversea.controller;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.MeituanConstants;
import hotel.base.oversea.utils.CollectionUtil;
import hotel.base.oversea.utils.GsonUtils;
import hotel.base.oversea.utils.Result;
import hotel.callback.oversea.config.service.RedisService;
import hotel.callback.oversea.service.MtHotelGoodsService;
import hotel.callback.oversea.task.MeituanPartTimeDealStatusVSecTask;
import hotel.callback.oversea.task.MeituanPartTimeQueryStatusVSecTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 第二版 美团酒店产品价格Controller
 * @author qiuyy
 * @date 2021-04-20
 */
@Slf4j
@RestController
@RequestMapping("/mtHotelGoodsPriceCallbackVSec")
@Api(value = "MtHotelGoodsPriceCallbackVSecController", tags = {"美团酒店产品价格接口"})
public class MtHotelGoodsPriceCallbackVSecController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    MeituanPartTimeQueryStatusVSecTask meituanPartTimeQueryStatusVSecTask;

    @Autowired
    MeituanPartTimeDealStatusVSecTask meituanPartTimeDealStatusVSecTask;

    @Autowired
    MtHotelGoodsService mtHotelGoodsService;

    @Autowired
    RedisService redisService;

    @PostMapping("/partTimeQueryStatusInfo")
    @ApiOperation(value = "节点时间定时器主动查询1个月产品房态",notes="必须经过auth授权")
    public Result<Object> partTimeQueryStatusInfo(@RequestBody Map map){
        Long queueSize = redisService.listSize(MeituanConstants.REIDS_POP_ALL_SALE_ROOM_ABOUT_MT_HOTEL_IDS_FOR_RLSTATUS_VSEC);
        log.info("executeQueryHotelGoodsStatus  待查询酒店队列={}",queueSize);
        if(queueSize==0){
            log.info("executeQueryHotelGoodsStatus 异常 queueSize == 0");
            return new Result<>("异常 queueSize == 0 ", CommonConstants.REQUEST_ERROR);
        }

        Integer daysCommon = (Integer) map.get("daysCommon");

        int secCountTask = MeituanConstants.PART_STATUS_TIME_TASK_COUNT;//100 定时器和该方法中都写这段代码：为了运行中，随时调整
        if(null != redisService.get("PartTimeQueryCheckStatusVSecJob_count_task")){
            secCountTask = (Integer) redisService.get("PartTimeQueryCheckStatusVSecJob_count_task");
        }

        //1秒n个 外部job间隔1秒调用该方法 //1秒100个//上限120个
        List<String> popList = new ArrayList<>();
        for (int i = 0; i < secCountTask; i++) {
            Integer hotelIdSplit = (Integer) redisService.RPop(MeituanConstants.REIDS_POP_ALL_SALE_ROOM_ABOUT_MT_HOTEL_IDS_FOR_RLSTATUS_VSEC);//优势酒店所有ids
            if(ObjectUtils.isEmpty(hotelIdSplit)){
                log.info("该批次[request]executeQueryHotelGoodsStatus 队列已无数据，i={},hotelIdSplit={}",i, GsonUtils.GsonString(hotelIdSplit));
                break;
            }else{
                popList.add(String.valueOf(hotelIdSplit));
            }
        }

        if(ObjectUtils.isEmpty(popList)){
            log.info("executeQueryHotelGoodsStatus 无可处理数据 popList ");
            return new Result<>();
        }
        log.info("进入executeQueryHotelGoodsStatus，size={},popList={},daysCommon={}",popList.size(),GsonUtils.GsonString(popList),daysCommon);
        meituanPartTimeQueryStatusVSecTask.executeQueryHotelGoodsStatus(popList,daysCommon);
        return new Result();
    }

    @PostMapping("/dealQueryStatusData")
    @ApiOperation(value = "解析-独立房态-接口数据-判断是否发生房态变化",notes="必须经过auth授权")
    public Result<Object> dealQueryStatusData(@RequestBody Map map){
        Long size = redisService.listSize(MeituanConstants.REIDS_MT_API_STATUS_DATA_QUEUE);
        log.info("dealQueryStatusData 当前待分析数据队列={}",size);
        if(size>0){
            Integer countTask = 10;//十个发起一个线程
            if(null != redisService.get("MeituanPartTimeDealStatusVSecTask_countTask")){
                countTask = (Integer)redisService.get("MeituanPartTimeDealStatusVSecTask_countTask");
            }
            List<String> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                String response = (String)redisService.RPop(MeituanConstants.REIDS_MT_API_STATUS_DATA_QUEUE);
                if(ObjectUtils.isEmpty(response)){
                    log.info("dealQueryStatusData 队列已无数据");
                    break;
                }
                list.add(response);
                if(BigDecimal.valueOf(list.size()).compareTo(BigDecimal.valueOf(countTask))==0){
                    log.info("dealQueryStatusData i={} 每{}个 处理数据list={}",i,countTask,GsonUtils.GsonString(list));
                    meituanPartTimeDealStatusVSecTask.executeDealQueryStatusData(list);
                    list = new ArrayList<>();
                }
            }
            if(!ObjectUtils.isEmpty(list)){
                log.info("dealQueryStatusData countTask={} 最后 处理数据list={}",countTask,GsonUtils.GsonString(list));
                meituanPartTimeDealStatusVSecTask.executeDealQueryStatusData(list);
            }
        }else{
            log.info("dealQueryStatusData 队列无数据可处理");
        }
        log.info("dealQueryStatusData 该批次结束 还有待分析队列size={}",redisService.listSize(MeituanConstants.REIDS_MT_API_STATUS_DATA_QUEUE));
        return new Result();
    }

}
